/*console.log("hello world");
function foo () {
	console.log("function foo");
	return 100;
}

var bar = "This is a pen.";
var http = require("http");
var i = foo();
console.log(i);*/


// var http = require("http");
// var testModule = require("./testModule");
// console.log(module.filename);

// testModule.getInfo();


var fs = require("fs");
var dataStr = "这是第一行。\r\n这是第二行。";
var dataBuf = new Buffer("这是第一行。\r\n这是第二行。");
var dataAppend = "这是追加的数据";
var options = {
	flag: "a"
}
var path = require('path');

/*图片的复制*/
/*fs.readFile("./img/bg.png", 'base64', function(err, data){
	console.log(data);
	fs.writeFile("./img/bgCopy.png", data.toString(), 'base64', function(err){
		console.log(data);
		if(err) console.log("error");
		else console.log("success");
	})
});*/



/*追加1*/
/*fs.appendFile("./messageBuf.txt", "好样的", function(err){
	if(err) console.log("error");
	else console.log("success");
})*/

/*追加2*/
/*fs.writeFile("./messageBuf.txt", dataAppend, options, function(err){
	if(err) console.log("error");
	else console.log("success");
})*/


/*指定位置read，要先open*/
/*fs.open("./message.txt", "r", function(err,fd){
	var buf = new Buffer(21);
	buf.fill(0);
	fs.read(fd, buf, 6, 9, 6, function(err, bytesRead, buffer){
		console.log(buf, buf.toString(), buf.slice(0,bytesRead).toString());
	})
})*/

/*fs.open('./message.txt', "r", function(err, fd){

	var buf = new Buffer(21);
	var bytesRead = fs.readSync(fd, buf, 0, 9, 3);
	console.log(bytesRead);
	console.log(buf, buf.toString(), buf.slice(0,bytesRead).toString());
});*/


/*var fd = fs.openSync("./message.txt", "r");
var buf = new Buffer(9);
var bytesRead = fs.readSync(fd, buf, 0, 9, 3);
console.log(bytesRead);
console.log(buf, buf.toString(), buf.slice(0,bytesRead).toString());*/


/*指定位置write，要先open*/
/*var buf = new Buffer("我喜爱编程");
fs.open("./message.txt", "w", function(err, fd){
 	fs.write(fd, buf, 3, 6, 3, function(err, written, buffer){
 		console.log(written);
 	})
});*/

/*指定位置写入，再追加， position为null*/
/*var buf = new Buffer("我喜爱编程");
fs.open("./message.txt", "a", function(err, fd){
 	fs.write(fd, buf, 3, 9, 0, function(err, written, buffer){
 		fs.write(fd, buf, 12, 3, null, function(err, written, buffer){

 		})
 	})
});*/

/*创建目录*/
/*fs.mkdir("../test", function(err){

});*/


/*读取目录*/
/*fs.readdir("./", function(err, files){
	console.log(files);
})*/


/*查看文件或目录的信息*/
/*fs.stat("./message.txt", function(err, stats){
	console.log(stats);
})*/



/*在open或openSync打开文件并返回文件描述后，可使用fstat查询文件打开信息*/
/*fs.open("./message.txt", "r", function(err, fd){
	fs.fstat(fd, function(err,stats){
		console.log(stats);
	})
});*/


/*检查文件或目录是否存在*/
/*fs.exists("./message.txt", function(exists){
	console.log(exists);
})
*/


/*获取绝对路径*/
/*var cache = {"/lib":"/node_modules/npm/lib"};
fs.realpath("./message.txt", function(err, resolvedPath){
	if(err) console.log(err);
	else console.log(resolvedPath);
})*/

/*修改文件访问时间、修改时间*/
/*fs.utimes("./message.txt", new Date(), new Date(), function(err){
	if(err) console.log(err);
	else console.log("success");
})*/


/*移动文件或目录*/
/*fs.rename("./message.txt", "./test/test.txt",function(err){
	if(err) console.log(err);
	else console.log("success");
});*/


/*创建与删除文件的硬链接*/
/*fs.link('./message.txt','./img/test.txt', function(err){
	if(err) console.log(err);
	else console.log("success");
})*/

/*fs.unlink('./img/test.txt', function(err){
	if(err) console.log(err);
	else console.log("success");
})*/


/*创建与查看文件的符号链接*/
/*fs.symlink(__dirname + '/message.txt', __dirname + '/img/test.txt', 'file', function(err){
	if(err) console.log(err);
	else{
		fs.readlink('./img/test.txt', function(err, linkString){
			if(err) console.log(err);
			else console.log(linkString);
		})
	}
})*/


/*截断文件*/
/*fs.truncate('./file.txt', 1000, function(err){
	if(err) console.log(err);
	else{
		fs.stat('./file.txt', function(err, stats){
			if(err) console.log(err);
			else console.log('the file size is ' + stats.size + ' bytes');
		});
	}
})*/


/*删除空目录*/
/*fs.rmdir('./emptyDir', function(err){
	if(err) console.log(err);
	else console.log("success");
})*/



/*监视文件或目录*/
// 如果在运行本段代码之前message.txt是不存在，创建后，prev的3个time都是
// atime: Thu Jan 01 1970 08:00:00 GMT+0800 (中国标准时间),
// mtime: Thu Jan 01 1970 08:00:00 GMT+0800 (中国标准时间),
// ctime: Thu Jan 01 1970 08:00:00 GMT+0800 (中国标准时间) 
// 所以输出结果是：the file is created
// 如果在运行本段代码之前message.txt是存在的，那么被删除后，再创建，prev的3个time是删除前的信息
// 输出结果是:the file is modified


/*var func1 = function(curr, prev){
	if(Date.parse(prev.ctime) == 0)
		console.log("the file is created");
	else if(Date.parse(curr.ctime) == 0)
		console.log("the file is deleted");
	else if(Date.parse(curr.mtime) != Date.parse(prev.mtime))
		console.log("the file is modified");
	console.log(curr);
	console.log(prev);
}

var func2 = function(curr, prev){
	if(Date.parse(curr.ctime) != 0){
		console.log("file size is " + curr.size);
	}
}*/

// 可以使用多个watchFile方法监视同一个文件
// {persistent:true, interval:1000}
/*fs.watchFile('./message.txt', func1);
fs.watchFile('./message.txt', func2);
fs.unwatchFile('./message.txt', func1);*/


/*var watcher1 = fs.watch('./file.txt', function(event, filename){
	console.log(event);
	console.log(filename);
	watcher1.close();
})*/

/*var watcher2 = fs.watch('./file.txt');
watcher2.on('change', function(event, filename){
	console.log(event);
	console.log(filename);
})*/







/*使用ReadStream对象读取文件*/
/*var readStream = fs.createReadStream('./message.txt', {start:3, end:12});
// 这里先输出open file, and start to read， 等待2秒后，开始读取文件，输出read data:...
readStream.pause();
setTimeout(function(){
	readStream.resume();
}, 2000);
readStream.on('open', function(fd){
	console.log("open file, and start to read");
});
readStream.on('data', function(data){
	console.log("read data: " + data);
});
readStream.on('close', function(){
	console.log("file is closed");
});
readStream.on('error', function(err){
	console.log(err);
});*/



/*使用WriteStream对象写入文件 : write end*/
// 实现message.txt的内容复制到anotherMessage.txt
/*var read = fs.createReadStream('./message.txt');
var out = fs.createWriteStream('./anotherMessage.txt');
read.on('data', function(data){
	out.write(data, function(){
		console.log(data.toString());
	});
});
out.on('open', function(fd){
	console.log("需要被写入的文件已被打开");
});
// 读取完所有数据触发，end事件的触发意味着data事件不再被触发
read.on('end', function(){
	// 在anotherMessage.txt追加goodbye字符串
	// 当没有数据再被写入流中时调用end方法
	out.end('goodbye', function(){
		console.log("文件全部写入完毕, 共写入%d字节数据", out.bytesWritten);
	});
});*/



/*观察WriteStream对象的write方法的返回结果并监听drain事件*/
/*var out = fs.createWriteStream("./test1.txt");
for(var i=0; i<=10000; i++){
	var flag = out.write(i.toString());
	console.log(flag);
	// true: 操作系统缓存区中数据未满，还可以写入数据; 
	// false: 操作系统缓存区中数据已满，并将立即输出到目标对象中;
}
out.on('drain', function(){
	console.log("操作系统缓存区中的数据已全部输出11。");
	var out = fs.createWriteStream('./test2.txt');
	for(var i=1; i<=10; i++){
		var flag = out.write(i.toString());
		console.log(flag);
	}
	out.on('drain', function(){
		console.log("操作系统缓存区中的数据已全部输出22。");
	})
});*/




/*观察drain事件的触发时机*/
/*var readStream = fs.createReadStream('./W3Cfuns_JavaScript_DOM编程艺术第二版（中文）.pdf');
var out = fs.createWriteStream('./W3Cfuns_JavaScript_DOM编程艺术第二版（中文）copy.pdf');
readStream.on('data', function(data){
	var flag = out.write(data);
	console.log(flag, out.bytesWritten);
});
out.on('drain', function(){
	console.log("操作系统缓存区中的数据已全部输出。");
});*/



/*观察WriteStream对象error事件*/
/*var out = fs.createWriteStream('./message.txt');
out.on('error', function(err){
	console.log(err);
});
out.write("我喜爱编程");
out.end(); //end方法被调用后，将不能继续在目标对象中写入数据。
out.write("node.js");  // 引发error*/


/*使用pipe方法复制文件*/
/*var file = fs.createReadStream('./message.txt');
var out = fs.createWriteStream('./anotherMessage.txt');
file.pipe(out);*/


/*将pipe方法的end属性值设置为false，以在目标文件中追加数据*/
/*var file = fs.createReadStream('./message.txt');
var out = fs.createWriteStream('./anotherMessage.txt');
file.pipe(out, {end: false});
file.on('end', function(){
	out.end("goodbye");
});*/


/*使用unpipe方法取消目标文件的写入操作*/
/*var file = fs.createReadStream('./W3Cfuns_JavaScript_DOM编程艺术第二版（中文）.pdf');
var out = fs.createWriteStream('./W3Cfuns_JavaScript_DOM编程艺术第二版（中文）copy.pdf');
file.pipe(out, {end: false});
setTimeout(function(){
	file.unpipe(out);
	out.end();
},10);*/


/*使用normalize方法解析路径*/
// F:\GitCK\xx-fontendTest\test-nodejs\myApp\file\a\b\c
/*var myPath = path.normalize('.//a//b//d//..//c/e//..//');
console.log(myPath);
var file = fs.createReadStream(myPath + 'message.txt');
file.on('data', function(data){
	console.log(data.toString());
});*/



/*使用join方法将参数值字符串结合为一个路径字符串*/
/*var myPath = path.join(__dirname, 'a', 'b', 'c');
console.log(myPath);
var file = fs.createReadStream(myPath + '/message.txt');
file.on('data', function(data){
	console.log(data.toString());
});
*/

/*使用resolve方法获取绝对路径*/
/*var myPath = path.resolve('a', 'b', 'c');
console.log(myPath);
var file = fs.createReadStream(myPath + '/message.txt');
file.on('data', function(data){
	console.log(data.toString());
});
*/

/*在resolve方法的最后一个参数中指定文件名*/
/*var myPath = path.resolve('a', 'b', 'c/message.txt');
console.log(myPath);
var file = fs.createReadStream(myPath);
file.on('data', function(data){
	console.log(data.toString());
});*/


/*relative方法*/
/*var myPath1 = path.relative(__dirname + '\\test' , __dirname + '\\a\\b\\c');
console.log(myPath1);

var myPath2 = path.relative('\\test', '\\a\\b\\c');
console.log(myPath2);

var myPath3 = path.relative('/a/xx.txt', '/a/b/c/message.txt');
console.log(myPath3);

var myPath4 = path.relative('../a\\xx.txt', '../a/b/c\\message.txt');
console.log(myPath4);*/




/*dirname方法*/
/*var myPath1 = path.dirname('./a/b/c');
console.log(myPath1);

var myPath2 = path.dirname('./a/b/c/message.txt');
console.log(myPath2);

var myPath3 = path.dirname('/a/b/c');
console.log(myPath3);

var myPath4 = path.dirname('/a/b/c/message.txt');
console.log(myPath4);

var myPath5 = path.dirname('c:\\a\\b\\c');
console.log(myPath5);

var myPath6 = path.dirname('c:\\a\\b\\c\\message.txt');
console.log(myPath6);*/


/*basename方法*/
/*var myPath1 = path.basename('/a/b/c/message.txt');
console.log(myPath1);

var myPath2 = path.basename('./a/b/c/message.txt', '.txt');
console.log(myPath2);

var myPath3 = path.basename('./a/b/c/message.txt', 'txt');
console.log(myPath3);

var myPath4 = path.basename('./a/b/c/message.txt', '.html');
console.log(myPath4);*/

// message.txt
// message
// message.
// message.txt



/*extname方法*/
/*var myPath1 = path.basename('/a/b/c/message.txt');
console.log(myPath1);

var myPath2 = path.basename('./a/b/c/message.');
console.log(myPath2);

var myPath3 = path.basename('./a/b/c/message');
console.log(myPath3);*/

// message.txt
// message.
// message